﻿function getOrgTree() {
	var treeloader = new Ext.tree.TreeLoader({
		dataUrl : 'init.action',
		listeners : {
			'beforeload' : function(treeLoader, node) {
				if (node.parentNode != null) {
					treeLoader.dataUrl = 'getSubOrg.action?node=' + node.id; // 定义子节点的Loader
					/*if (node.isLeaf()) {
						// node.icon =
						// 'extjs/resources/images/default/tree/folder.gif';
					}*/
				} else {
					treeLoader.dataUrl = 'init.action';
				}
			},
			'load' : function(treeLoader, node, response) {
				var responseTextVale = Ext.util.JSON
						.decode(response.responseText);
				if (node.parentNode == null) {
					node.setText(Ext.util.JSON
							.decode(responseTextVale.jsonString)[0].text);
					node.id = Ext.util.JSON.decode(responseTextVale.jsonString)[0].id;
					node.appendChild(Ext.util.JSON
							.decode(responseTextVale.jsonString)[0].children);
				} else {
					node.appendChild(Ext.util.JSON
							.decode(responseTextVale.jsonString));
				}

			}
		}
	});

	var root = new Ext.tree.AsyncTreeNode({
		// text: 'Test',
		draggable : false,
		id : 'root',
		expanded : true,
		singleClickExpand : false
			// children:[{id:'childnode1',text:'子节点一',leaf:true},{id:'childnode2',text:'子节点二',children:[{id:'childnode3',text:"111",leaf:true}]}]

		});

	var treeMask = new Ext.LoadMask(Ext.getBody(), {
				msg : "数据加载中，请稍候..."
			});

	return new Ext.tree.TreePanel({
				title : '组织架构',
				border : false,
				
				useArrows : true,
				autoScroll : true,
				animate : true,
				enableDD : true,
				containerScroll : true,
				root : root,
				lines : true,
				loader : treeloader,
				listeners : {
					'beforeload' : function(node) {
						treeMask.show();
					},
					'load' : function(node) {
						treeMask.hide();
					}
				},
				frame : true
			});
}
